using System;
using System.Linq;
using System.Windows.Forms;
using HomeDms.Properties;
using ImageAcquisitionTAL;
using System.Globalization;
using System.Collections.Generic;
using HomeDmsUtils;
using System.Threading;
using System.Reflection;
using HomeDmsPageStorageProviders;

namespace HomeDms
{
    public partial class PreferencesForm : Form
    {
        public PreferencesForm()
        {
            DocumentListForm.TheDocumentListForm.Cursor = Cursors.WaitCursor;
            InitializeComponent();
        }

        private void PreferencesForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == DialogResult.Cancel)
                return;
            if (e.Cancel)
                return;            
            if (!ValidateChildren())
            {
                e.Cancel = true;
                return;
            }
            // save settings...            
            Settings.Default.DefaultScanner = scannersCombobox1.SelectedDeviceID;
            if (comboBox1.SelectedItem != null)
                Settings.Default.DefaultMainDocumentLanguage = (comboBox1.SelectedItem as CultureInfo).Name;
            else
                Settings.Default.DefaultMainDocumentLanguage = CultureInfo.CurrentCulture.Name;
            Settings.Default.DefaultResolution = resolutionsComboBox.SelectedResolution;
            Settings.Default.DefaultColorMode = colorsComboBox1.SelectedColorMode;
            Settings.Default.Save();
            PageStorage.SaveConfiguration();
        }

        private void PreferencesForm_Load(object sender, EventArgs e)
        {
            foreach (PageStorageProvider p in PageStorage.Providers)
            {
                int idx = checkedListBox1.Items.Add(p);
                checkedListBox1.SetItemChecked(idx, p.Active);
            }
            label7.Text = PageStorage.MainProvider.Description;
            listBox1.SelectedIndex = Settings.Default.UILanguage;
            listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
            var q = from cult in CultureInfo.GetCultures(CultureTypes.AllCultures)
                    orderby cult.DisplayName
                    select cult;
            comboBox1.Items.Clear();
            comboBox1.Items.AddRange(q.ToArray<CultureInfo>());
            comboBox1.SelectedItem = CultureInfo.GetCultureInfo(Settings.Default.DefaultMainDocumentLanguage);
            if (Settings.Default.DefaultMainDocumentLanguage == "")
                comboBox1.SelectedItem = CultureInfo.CurrentCulture;
            UpdateCombos();
            resolutionsComboBox.SelectedResolution = Settings.Default.DefaultResolution;
            colorsComboBox1.SelectedColorMode = Settings.Default.DefaultColorMode;
            this.Activate(); // this is because of some ugly splash screens the datasource can present on start
            DocumentListForm.TheDocumentListForm.Cursor = Cursors.Default;
        }

        private void UpdateCombos()
        {
            ScannerInfo sinfo = scannersCombobox1.SelectedScannerInfo;
            if (sinfo != null)
            {
                resolutionsComboBox.Resolutions = sinfo.SupportedResolutions;
                colorsComboBox1.SetColorsFromScannerInfo(sinfo);
            }
        }

        private void scannersCombobox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateCombos();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Localizer.MsgBox(this, "Application will be restarted. Click Yes to continue", "Restart", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) != DialogResult.Yes)
                return;
            Settings.Default.UILanguage = listBox1.SelectedIndex;
            DialogResult = DialogResult.Retry;            
        }

        private void checkedListBox1_Format(object sender, ListControlConvertEventArgs e)
        {
            e.Value = (e.ListItem as PageStorageProvider).Description;
        }

        private void groupBox4_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            button1.Enabled = checkedListBox1.SelectedItem != null;            
            button2.Enabled = button1.Enabled && (checkedListBox1.SelectedItem != PageStorage.MainProvider);            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (checkedListBox1.SelectedItem != null)
            {
                PageStorage.MainProvider = checkedListBox1.SelectedItem as PageStorageProvider;
                button2.Enabled = false;
                label7.Text = PageStorage.MainProvider.Description;
            }
        }

        private void checkedListBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            bool atLeast1Active = false;
            foreach (PageStorageProvider p in checkedListBox1.Items)
            {
                p.Active = checkedListBox1.CheckedItems.Contains(p);
                if (p.Active)
                    atLeast1Active = true;
            }
            e.Cancel = !(atLeast1Active && PageStorage.MainProvider.Active);
            if (!atLeast1Active)
                Localizer.MsgBox(this, "At least one page storage option must be active", "Configuration error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            if (!PageStorage.MainProvider.Active)
                Localizer.MsgBox(this, "Main page storage option must be active", "Configuration error", MessageBoxButtons.OK, MessageBoxIcon.Warning);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (checkedListBox1.SelectedItem != null)
            {
                PageStorage.ShowConfigurationDialog((checkedListBox1.SelectedItem as PageStorageProvider).Name, this);
            }
        }

    }
}